﻿using System.ComponentModel;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web.Mvc;
using Web.Models;
using Web.Models.ViewModels;

namespace Web.Areas.ManagerArea.Controllers
{
    [Description("AdminNavigationController说明")]
    public class AdminNavigationController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        #region 获取列表============================================================

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentid"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="type">0表示树状，1表示右侧列表</param>
        /// <returns></returns>
        public ActionResult List(int? parentid, int page = 1, int pagesize = 20, int type = 0)
        {
            using (var db = MyDbContext.Create())
            {
                IQueryable<AdminNavigation> list = db.AdminNavigations.OrderBy(m => m.OrderId);
                if (null != parentid) list = list.Where(m => m.ParentId == parentid.Value);
                if (type == 0) return new LowerCamelJsonResult(list.ToList());
                DbDataResult result = new DbDataResult
                {
                    CurrentPage = page,
                    PageSize = pagesize,
                    RecordCount = list.Count(),
                    Data = list.Skip((page - 1) * pagesize).Take(pagesize).ToList(),
                    StatusCode = 0
                };
                return new LowerCamelJsonResult(result);
            }
        }
        #endregion

        public ActionResult Edit(int? id)
        {
            using (var db = MyDbContext.Create())
            {
                ViewBag.ParentId= new SelectList(db.AdminNavigations.Where(m=>m.ParentId==null).ToList(),"Id","Text");
                if (null == id) return View();
                var model = db.AdminNavigations.Find(id.Value);
                if (null == model) return HttpNotFound();
                return View(model);
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(AdminNavigation model)
        {
            DbResult result = new DbResult();
            if (ModelState.IsValid)
            {
                using (var db = MyDbContext.Create())
                {
                    db.AdminNavigations.AddOrUpdate(model);
                    db.SaveChanges();
                    result.StatusCode = 0;
                    result.Message = "success";
                }
            }
            else
            {
                AddErrors(out result);
            }
            return new LowerCamelJsonResult(result);
        }

        public ActionResult Delete(int? id)
        {
            DbResult result = new DbResult();
            if (null == id)
            {
                result.Message = "参数错误";
            }
            else
            {
                using (var db = MyDbContext.Create())
                {
                    var model = db.AdminNavigations.Find(id.Value);
                    if (null == model) result.Message = "未找到该记录";
                    else
                    {
                        db.AdminNavigations.Remove(model);
                        db.SaveChanges();
                        result.Message = "已成功删除";
                        result.StatusCode = 0;
                    }
                }
            }

            return new LowerCamelJsonResult(result);
        }
    }
}
